package jmine.tec.hydra.services.incluir;

import jmine.tec.database.pack.PackException;
import jmine.tec.hydra.domain.specification.HydraScheduledSpecification;
import jmine.tec.hydra.domain.specification.HydraSpecificationImpl;
import jmine.tec.hydra.exception.HydraException;
import jmine.tec.services.annotations.Input;
import jmine.tec.services.annotations.ServiceImplementor;
import jmine.tec.services.api.ActionsEnum;
import jmine.tec.services.api.export.ReferenceMap;
import jmine.tec.services.export.impl.ExportUtils;
import jmine.tec.services.io.ServiceBean;

import org.quartz.CronExpression;

/**
 * Serviço de inclusão de uma especificação de integração.
 * 
 * @author piercio
 * @deprecated use jmine.tec.scheduling
 */
@Deprecated
@ServiceImplementor(action = ActionsEnum.INCLUIR)
public class HydraScheduledSpecificationService extends HydraSpecificationService<HydraScheduledSpecification> {

    public static final String CRON_EXPRESSION = "CRON Expression";

    private String cronExpression;

    /**
     * {@inheritDoc}
     */
    @Override
    protected HydraSpecificationImpl findOrCreate() throws PackException {
        if (!CronExpression.isValidExpression(this.cronExpression)) {
            throw new HydraException("jmine.tec.hydra.specification.schedule.invalid.cronexpression", this.getAction(), this.getSystem(),
                    this, this.cronExpression);
        }
        HydraScheduledSpecification bean = this.getHelper().findOrCreate(HydraScheduledSpecification.class, this.getDatabaseID(), null);
        bean.setCronExpression(this.cronExpression);
        return bean;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void fillServiceBean(ServiceBean bean, ReferenceMap referenceMap, HydraScheduledSpecification entity) {
        super.fillServiceBean(bean, referenceMap, entity);
        ExportUtils.put(bean, CRON_EXPRESSION, entity.getCronExpression());
    }

    /**
     * @param cronExpression o valor para o campo cronExpression
     */
    @Input(fieldName = CRON_EXPRESSION)
    public void setCronExpression(String cronExpression) {
        this.cronExpression = cronExpression;
    }

}
